//
//  BookMeeeting.swift
//  GOFM
//
//  Created by siri on 2024/10/21.
//  Copyright © 2024 Glad. All rights reserved.
//  人员闲忙时间选择

import SwiftUI
import Combine
import SwiftUIRefresh
import Factory

struct IdleTimeView: View {
    
    
    @State private var scrollToIndex: Int?


    // 定义触发器
    final class Triggers: ObservableObject {
        var load = PassthroughSubject<Void, Never>()
        var reload = PassthroughSubject<Void, Never>()
        var select = PassthroughSubject<Void, Never>()
    }
    
    @StateObject var viewModel: IdleTimeViewModel
    @StateObject var output: IdleTimeViewModel.Output
    @StateObject var cancelBag: CancelBag
    @StateObject var triggers: Triggers

    var body: some View {
        NavigationView {
            VStack {
                IdleTimeNavigationBar(title: "已选十人", rightTitle: "确定", showLeftArrow: true)
                IdleTimeHead()
                IdleTimeWeek(scrollToIndex: $scrollToIndex)
                IdleTimeMain(scrollToIndex: $scrollToIndex)
                Spacer()
            }.frame(minWidth: 0, maxWidth:  UIScreen.main.nativeBounds.width, minHeight: 0, maxHeight:  UIScreen.main.nativeBounds.height).background(Color.g_color_bg_grey)
        }
     }
    init(viewModel:IdleTimeViewModel) {
        let triggers = Triggers()
        let cancelBag = CancelBag()
        
        let input = IdleTimeViewModel.Input(
            loadTrigger: triggers.load.eraseToAnyPublisher(),
            reloadTrigger: triggers.reload.eraseToAnyPublisher(),
            selectTrigger: triggers.select.eraseToAnyPublisher()
        )
        
        let output = viewModel.transform(input, cancelBag: cancelBag)
        
        self._viewModel = StateObject(wrappedValue: viewModel)
        self._output = StateObject(wrappedValue: output)
        self._cancelBag = StateObject(wrappedValue: cancelBag)
        self._triggers = StateObject(wrappedValue: triggers)
    }

    
}

struct IdleTimeView_Preview: PreviewProvider {
    static var previews: some View {
        IdleTimeView(viewModel:IdleTimeViewModel(navigationController: UINavigationController()))
    }
}

extension Container {
    func idleTimeView(navigationController: UINavigationController) -> Factory<IdleTimeView> {
        Factory(self) {
            IdleTimeView(viewModel:IdleTimeViewModel(navigationController: navigationController))
        }
    }
}
